package edu.guet.server.service;

import edu.guet.server.mapper.CommentMapper;
import edu.guet.server.mapper.PostMapper;
import edu.guet.server.mapper.SemesterMapper;
import edu.guet.server.mapper.UserMapper;
import edu.guet.server.model.Comment;
import edu.guet.server.model.Post;
import edu.guet.server.model.Resource;
import edu.guet.server.model.User;
import edu.guet.server.model.dto.TechPostDTO;
import edu.guet.server.model.vo.CommentVO;
import edu.guet.server.utils.ResultAPI;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.Iterator;
import java.util.List;


@Service
@Transactional
public class PostService {

    @Autowired
    private PostMapper postMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private CommentMapper commentMapper;
    @Autowired
    private SemesterMapper semesterMapper;

    public ResultAPI hottestPost() {
        List<Post> postlist = postMapper.hottestPost();
        Iterator<Post> iterator = postlist.iterator();

        while (iterator.hasNext()) {
            Post post = iterator.next();

            User u = userMapper.getUserById(post.getPublisher_id());
            post.setImg_url(u.getImg_url());
            post.setUser_name(u.getUsername());
            post.setComment_count(commentMapper.commentCount(post.getPost_id()));

        }
        return ResultAPI.success(postlist, "成功获取热门问题");

    }

    public ResultAPI setPost(Post post) {
        post.setCourse_id(semesterMapper.getCIdBysemId(post.getSemester_id()));
        postMapper.setPost(post);
        System.out.println(post);
        return ResultAPI.success(post, "话题设置成功");


    }

    public ResultAPI getPost(Integer semester_id) {
        List<TechPostDTO> posts = postMapper.getPost(semester_id);
        return ResultAPI.success(posts, "获取话题成功");
    }

    public ResultAPI newestPost() {
        List<Post> postlist = postMapper.newestPost();
        Iterator<Post> iterator = postlist.iterator();
        while (iterator.hasNext()) {
            Post post = iterator.next();

            if (CollectionUtils.isEmpty(commentMapper.selectComment(post.getPost_id()))) {
                iterator.remove(); // 使用迭代器删除元素，避免ConcurrentModificationException
            }
            if (commentMapper.firstCommentByTime(post.getPost_id()) != null) {
                Comment comment = commentMapper.firstCommentByTime(post.getPost_id());
                post.setNewCommentContent(comment.getContent());
                User user = userMapper.getUserById(comment.getUser_id());
                post.setImg_url(user.getImg_url());
                post.setUser_name(user.getUsername());
                post.setPublisher_id(user.getId());//这里是评论发布人的id而非评论区发布人的id
                post.setComment_count(commentMapper.commentCount(post.getPost_id()));
            }

        }

        return ResultAPI.success(postlist, "成功获取最新评论");
    }

    public ResultAPI getPostReply(Integer semesterId, Integer publisherId) {
        List<CommentVO> commentVOList=commentMapper.getPostReply(semesterId,publisherId);
        return ResultAPI.success(commentVOList);
    }
}
